package com.itheima.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Author 王加鑫
 * @Date 2021/5/25 15:52
 * @Version 1.0
 */
public class ServerDemo01 {
    public static void main(String[] args) throws IOException {
        ServerSocket socket = new ServerSocket(10001);
        while (true){
            //跟客户端建立连接
            Socket accept = socket.accept();
            //读取客户端发过来的资料
            InputStream is = accept.getInputStream();
            byte[] bytes = new byte[1024];
            int i;
            while ((i = is.read(bytes)) != -1){
                System.out.println(new String(bytes,0,i));
            }

            //给客户端传送数据
            OutputStream os = accept.getOutputStream();
            Scanner sc = new Scanner(System.in);
            String s = sc.next();
            os.write(s.getBytes());
            os.flush();

            //关闭os
            accept.shutdownOutput();

            //关闭资源
            os.close();
            accept.close();
        }

    }
}
